﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Alex.Library.Common
{
    /// <summary>
    /// 随机数Helper类
    /// </summary>
    public sealed class RandomHelper
    {
        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <param name="attachNum">参与计算的附加值</param>
        /// <returns></returns>
        public static int Create(int min, int max, int? attachNum)
        {
            //使用系统时钟计时周期作为随机数种子

            long tick = DateTime.Now.Ticks;

            int seed = (int)(tick & 0xffffffffL) | (int)(tick >> 32);

            //如果参数中有参与计算种子的整数就添加到种子中
            seed += attachNum ?? 0;

            Random ro = new Random(seed);

            if (min >= 0 && max > 0)
            {
                return ro.Next(min, max);
            }
            else if (max > 0)
            {
                return ro.Next(max);
            }
            else
            {
                return ro.Next();
            }
        }
    }
}
